<?php
// +----------------------------------------------------------------------
// | HkCms
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: HkCms team <admin@hkcms.cn>
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace app\index\model\cms;

use think\Model;

class Recommend extends Model
{
    /**
     * 获取站点模块列表
     * @param $tag
     * @return array|mixed|object|\think\App
     */
    public function getList($tag)
    {
        if (empty($tag) || empty($tag['name'])) {
            return [];
        }

        // 缓存设置
        $cacheTime = !empty($tag['cache']) && is_numeric($tag['cache']) ? intval($tag['cache']) : 3600;
        $cacheID = to_guid_string($tag);
        if (!env('APP_DEBUG') && $cacheData = cache($cacheID)) {
            return $cacheData;
        }

        $info = $this->where(['name'=>$tag['name'],'status'=>'normal'])->find();
        if (empty($info)) {
            return [];
        }

        if (!empty($tag['itemid']) && is_numeric($tag['itemid'])) {
            $banner = Banner::where(['recommend_id'=>$info->getAttr('id')])->where(['id'=>$tag['itemid']])->order('weigh','asc')->select()->toArray();
        } else {
            $banner = Banner::where(['recommend_id'=>$info->getAttr('id')])->order('weigh','asc')->select()->toArray();
        }

        if (!env('APP_DEBUG')) {
            cache($cacheID, $banner, $cacheTime);
        }
        return $banner;
    }
}